package com.kd.controller.recruiter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.kd.bean.admin.Admin_msg;
import com.kd.bean.jobSeeker.Basic_information;
import com.kd.bean.recruiter.Company_msg;
import com.kd.bean.recruiter.Recruiter_basic;
import com.kd.bean.recruiter.Recruitment_msg;
import com.kd.service.recruiter.ICompany_msgService;
import com.kd.service.recruiter.IRecruitment_msgService;
import com.kd.service.recruiter.impl.Company_msgServiceImpl;

@Controller
@RequestMapping("/recruitment_msg")
public class Recruitment_msgController {
	
	@Autowired
	private IRecruitment_msgService recService;
	
	@Autowired
	private ICompany_msgService comService;//招聘者相关的类 --公司信息（Company_msg）--

	
	@RequestMapping("/queryAllByRecruitment_msg")
	@ResponseBody
	public PageInfo<Recruitment_msg> queryAllByRecruitment_msg(Integer pageSize,Integer pageNum,
			Double rec_pay_min,Double rec_pay_max,Integer rec_workYear,String rec_company_address
			,Integer rec_qualification,String rec_duty){
		//* <默认分页显示所有岗位招聘信息（一次10条信息）。仅显示招聘岗位，薪资待遇，招聘公司，公司地址，工作经验年限，以及招聘者头像，名字。招聘信息状态为1的所有信息

		PageInfo<Recruitment_msg> info = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("rec_pay_min", rec_pay_min);
			map.put("rec_pay_max", rec_pay_max);
			map.put("rec_workYear", rec_workYear);
			map.put("rec_company_address", "%"+rec_company_address+"%");
			map.put("rec_qualification", rec_qualification);
			map.put("rec_duty", "%"+rec_duty+"%");
			info = recService.queryAllByRecruitment_msg(pageNum, pageSize,map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}
	
	
	@RequestMapping("/queryAllRecruitment_msg")
	@ResponseBody
	public PageInfo<Recruitment_msg> queryAllRecruitment_msg(Integer pageSize,Integer pageNum){
		//显示所有招聘信息
		PageInfo<Recruitment_msg> info = null;
		try {
			
			info = recService.queryAllRecruitment_msg(pageNum, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
			
	}
	@RequestMapping("/queryGGFaBuZhaoPingMessage")
	@ResponseBody
	public PageInfo<Recruitment_msg> queryGGFaBuZhaoPingMessage(Integer pageSize,Integer pageNum,HttpSession session){
		//查询刚刚发布的招聘信息审核状态为3的所有信息
		PageInfo<Recruitment_msg> info = null;
		try {
			info = recService.queryGGFaBuZhaoPingMessage(pageNum, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}
	
	@RequestMapping("/updateFaBuZhaoPingMessagRec_msg_check_state")
	@ResponseBody
	public Boolean updateFaBuZhaoPingMessagRec_msg_check_state(Integer rec_id,Integer rec_msg_check_state,HttpSession session){
		//管理员修改发布招聘信息的状态为审核通过
		Boolean flag = false;
		try {
			Admin_msg adminMsg = (Admin_msg) session.getAttribute("adminMsg");
			//修改审核状态管理员操作
			flag = recService.updateFaBuZhaoPingMessagRec_msg_check_state(rec_id, rec_msg_check_state, adminMsg.getAdmin_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	@RequestMapping("/queryJobCount")
	@ResponseBody
	public Integer queryJobCount() {
		//管理员   招聘人数
		Integer jobCount = 0;
		try {
			jobCount = recService.queryJobCount();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jobCount;
	}
	
	@RequestMapping("/queryNowJobCount")
	@ResponseBody
	public Integer queryNowJobCount() {
		//管理员   招聘人数
		Integer nowJobCount = 0;
		try {
			nowJobCount = recService.queryNowJobCount();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nowJobCount;
	}
	
	@RequestMapping("/queryAllRecMsgByRecruiter_id")
	@ResponseBody
	public PageInfo<Recruitment_msg> queryAllRecMsgByRecruiter_id(
			Integer pageNum,Integer pageSize,HttpSession session){
		PageInfo<Recruitment_msg> info = null;
		try {
			Recruiter_basic basicMs = (Recruiter_basic) session.getAttribute("basicMsg");
			info = recService.queryAllRecMsgByRecruiter_id(pageNum, pageSize, basicMs.getRecruiter_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}
	
	@RequestMapping("/removeRecMsgByRecruiter_id")
	@ResponseBody
	public Boolean removeRecMsgByRecruiter_id(Integer rec_id){
		Boolean flag = false;
		try {
			flag = recService.removeRecMsgByRecruiter_id(rec_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	@RequestMapping("/insertRecMsg")
	@ResponseBody
	public Map<String, Object> insertRecMsg(Recruitment_msg recMsg,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//招聘者信息
			Recruiter_basic basic =  (Recruiter_basic) session.getAttribute("basicMsg");
			if(basic.getRecruiter_statei()==1) {
				map.put("msg", "账号状态为禁用");
				return map;
			}
			//查看是否填写公司信息
			if(basic.getCompany_id()!=null) {
				//查询公司
				Company_msg company = comService.queryByCompany_id(basic.getCompany_id());
				if(company!=null) {
					Boolean flag = recService.insertRecMsg(recMsg,basic,company);
					if(flag) {
						map.put("msg", "发布成功");
						return map;
					}else {
						map.put("msg", "发布失败，请仔细检查");
						return map;
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map.put("msg", "必须填写所在公司信息，才可发布招聘信息");
		return map;
	}
	
	@RequestMapping("/queryByRec_id")
	public String queryByRec_id(Integer rec_id,Model model) {
		try {
			Recruitment_msg recruitment_msg = recService.queryByRec_id(rec_id);
			model.addAttribute("recruitment_msg", recruitment_msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/view/news.jsp";
	}
}
